package com.sen.rbac.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sen.common.common.controller.BaseController;
import com.sen.common.common.response.MessageResult;
import com.sen.common.common.response.ResultMsgEnum;
import com.sen.common.common.util.CheckUtils;
import com.sen.rbac.dto.SysTenantEntityTypeDTO;
import com.sen.rbac.feign.RbacEntityTypeFeign;
import com.sen.rbac.model.SysTenantEntityType;
import com.sen.rbac.service.TenantEntityTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 主体类型维护
 * @author esjiang
 */
@RestController
@RequestMapping("/tenantEntity")
public class TenantEntityTypeController extends BaseController implements RbacEntityTypeFeign {

    @Resource
    TenantEntityTypeService tenantEntityTypeService ;

    @PostMapping("/saveOrUpdate")
    public MessageResult saveOrUpdate(@RequestBody SysTenantEntityTypeDTO sysTenantEntityTypeDTO){
        return exec("保存或修改",sysTenantEntityTypeDTO,()->{
            if (StrUtil.isBlank(sysTenantEntityTypeDTO.getTenantCode())){
                CheckUtils.checkNotNull(sysTenantEntityTypeDTO,"entityTypeName","systemCode","tenantCode");
            }
            tenantEntityTypeService.saveOrUpdate(sysTenantEntityTypeDTO);
            return MessageResult.success() ;
        }) ;
    }

    @PostMapping("/list")
    public MessageResult list(@RequestBody SysTenantEntityTypeDTO sysTenantEntityTypeDTO){
        return exec("列表查询",sysTenantEntityTypeDTO,()->
           MessageResult.success(tenantEntityTypeService.list(sysTenantEntityTypeDTO))
        );
    }

    @GetMapping("/del")
    public MessageResult del(@RequestParam("id")String id){
        return exec("删除区划主体类型",id,()-> {
            tenantEntityTypeService.del(id);
            return MessageResult.success() ;
        }) ;
    }

    /**
     * 查询区划下开的主体类型
     * @param tenantCode
     * @return
     */
    @GetMapping("/listTenantEntityTypeByTenantCode")
    public MessageResult listTenantEntityType(@RequestParam("tenantCode") String tenantCode){
        return exec("查询区划下开的主体类型",tenantCode,false,()->{
            List<SysTenantEntityType> list = tenantEntityTypeService.list(Wrappers.<SysTenantEntityType>lambdaQuery().select(SysTenantEntityType::getSystemCode, SysTenantEntityType::getEntityType)
                    .eq(SysTenantEntityType::getTenantCode, tenantCode));
            Map<String,List<String>> result = new HashMap<>();
            if (CollUtil.isNotEmpty(list)){
               list.stream().map(record -> {
                    String systemCode = record.getSystemCode();
                    String entityType = record.getEntityType();
                    if (result.containsKey(systemCode)) {
                        result.get(systemCode).add(entityType);
                    } else {
                        List entityTypes = new LinkedList();
                        entityTypes.add(entityType);
                        result.put(systemCode, entityTypes);
                    }
                    return result;
                }).collect(Collectors.toList());
            }
            return MessageResult.success(result) ;
        }) ;
    }

    /**
     * 同步主体类型
     * @param entityCode
     * @param entityTypeCodes 多个code使用逗号分隔
     * @return
     */
    @Override
    @GetMapping("/syncEntityTypeRoles")
    public MessageResult syncEntityTypeRoles(@RequestParam("entityCode") String entityCode,@RequestParam("entityTypeCodes") String entityTypeCodes){
        return exec("同步主体类型","主体编码:"+entityCode+",主体类型:"+ entityTypeCodes,()->{
            if (StrUtil.isBlank(entityCode) || StrUtil.isBlank(entityTypeCodes)){
                return MessageResult.bizFailed(ResultMsgEnum.PARAM_IS_NULL) ;
            }
            tenantEntityTypeService.syncEntityTypeRoles(entityCode,entityTypeCodes);
            return MessageResult.success() ;
        }) ;
    }
}
